/*
Section: Alternative treatment definition with armed conflict events
Content: Table G.6
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/



********************************************************************************
**# LOAD DATASET
********************************************************************************

use "$intermediate/finaldata_main_ieb_noevent.dta", clear


********************************************************************************
**# ADD THE CONFLICT DATA
********************************************************************************
merge m:1 startmy ieb_nat_id using "$uppsala\prepostarmedconflict_ieb_final.dta", gen(merge_prepost_armedc) keep(1 3)

cap drop date2
g date2=startmy if treatment_armedc_m3y_p90==1
format date2 %tm
replace  date2=startmy+3 if treatment_armedc_m3y_p90==0


********************************************************************************
**# ADJUST DATASET AND REDUCE IT
********************************************************************************

keep if match_date != .
drop if year==.
keep if inrange(year,2000,2018)
keep if !inlist(ieb_nat_id,.,0)

g ysm_v2= int((endepi-entry_spell)/365)

rename ysm_atunemp ysm_old
rename ysm_v2 ysm_atunemp
label var ysm_atunemp "Years since mig."


cap drop allcontrols
g allcontrols=1 if  unemp_3m!=. & edu_max!=. & age !=. & frau!=. & ysm_atunemp!=. & ln_wage_any_lb!=. & ln_firmsize!=.  &  treatment_armedc_m3y_p90!=. & state!=. & woao_llm!=.
keep if allcontrols==1

* Ensure there is ppl in control and treated within the same year and state
cap drop date2
g date2=startmy if treatment_armedc_m3y_p90==1
format date2 %tm
replace  date2=startmy+3 if treatment_armedc_m3y_p90==0
g year2=year(dofm(date2))


foreach x in treatment_armedc_m3y_p90 {
cap drop max_`x' min_`x'
bysort ieb_nat date2 state: egen max_`x'=max(`x')
bysort ieb_nat date2 state: egen min_`x'=min(`x')
cap drop k_`x'_st
g k_`x'_st=1 if max_`x'==1 & min_`x'==0
cap drop max_`x' min_`x'
}

preserve
collapse (sum) treatment_armedc_m3y_p90, by(startmy ieb_nat_id)
save "$dataevent/events_armedconflict_ieb.dta", replace
restore


* Other controls
g quarter=0 if inrange(month,1,3)
replace quarter=1 if inrange(month,2,6)
replace quarter=2 if inrange(month,7,9)
replace quarter=3 if inrange(month,10,12)

replace edu_max=1 if edu_max==0 

* Standard errors
g cluster_se=ieb_nat_id*year*month
clonevar relevantarmedc_m3y_p90=startmy if treatment_armedc_m3y_p90==1
replace relevantarmedc_m3y_p90=startmy+3 if treatment_armedc_m3y_p90==0

egen clusterarmedc_m3y_p90=group(relevantarmedc_m3y_p90)

********************************************************************************
**# Table G.6
********************************************************************************
egen miss =  rowmiss(ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb)
	
eststo clear
local i=1
foreach out in  unemp_1m unemp_3m  {
foreach control in control control_ext {

global control = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp"
global control_ext = "i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb"

qui eststo mod`i': reghdfe `out' treatment_armedc_m3y_p90 ${`control'} if  k_treatment_armedc_m3y_p90_st==1 & register_unemp==1  & miss == 0, absorb(i.ieb_nat_id#i.year2 i.year2#i.month i.state)  vce(cluster clusterarmedc_m3y_p90)

sum	 `out' if k_treatment_armedc_m3y_p90_st==1 & register_unemp==1 & treatment_armedc_m3y_p90 == 0

estadd scalar ymean = r(mean)
estadd local `control' "Yes"
estadd local yearorigin "Yes"
estadd local yearmonth "Yes"
estadd local state "Yes"

local i=`i'+1

}
}
		
	
**# TABLE
esttab mod* using "$table/tableG6_final.tex", star(* 0.10 ** 0.05 *** 0.01) se replace b(3) drop(*edu_max age frau *ysm_atunemp ln_wage_any_lb ln_firmsize_lb full_time_lb share_for_lb_ab_med qualif_hq_lb_ab_med abovemedian_AKM_lb _cons) nocons  nogaps nonotes noob nomtitle ///
mgroups("Emp. within 1m" "Emp. within 3m", pattern(1 0 1 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
varlabel(treatment_armedc_m3y_p90 "Post-Conflict") scalars( "nobs Observations" "ymean Mean intention to stay"  "yearorigin Origin country x Year FE" "yearmonth Month FE x Year FE"  "state State of Residency FE" "control Indiv. Controls" "control_ext Extended Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)
